package com.igeekhome.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeekhome.biz.*;
import com.igeekhome.pojo.*;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 客户信息 前端控制器hhhhhhhhhh
 * </p>
 *
 * @author ${author}
 * @since 2021-03-16
 */
@Controller
@RequestMapping("/customerInfo")
public class CustomerInfoController {

    @Autowired
    private ICustomerServiceService iCustomerServiceService;

    @Autowired
    private ICustomerInfoService iCustomerInfoService;

    @Autowired
    private IWorkOrderService iWorkOrderService;

    @Autowired
    private IBlackListService iBlackListService;

    @Autowired
    private ISessionMessageService iSessionMessageService;


    public void initcustomerservice(Model model, Integer current,Integer size){

        IPage<CustomerInfo> page=new Page<>(current,size); //表
        IPage<CustomerInfo> page1=iCustomerInfoService.page(page);
        List<CustomerInfo> list=page1.getRecords();
        long pagesCount=page1.getPages();

        model.addAttribute("list",list);
        model.addAttribute("pagesCount",pagesCount);

    }

    @RequestMapping("/index")//@RequestParam("current") Integer current,@RequestParam("size") Integer size
    public String index(Model model, @RequestParam("value") String value, @RequestParam("current") Integer current, @RequestParam("size") Integer size){

        IPage<CustomerInfo> page=new Page<>(current,size); //表
        QueryWrapper<CustomerInfo> qw=new QueryWrapper<>();
//        qw.orderByDesc("lastedlogintime");
//        qw.like("realname",value);
        qw.like("realname",value).or().like("email", value).or().like("phone", value).or().like("level", value).or().like("state",value).or().like("customername",value);
        IPage<CustomerInfo> page1 = iCustomerInfoService.page(page, qw);
        model.addAttribute("list",page1.getRecords());
        long pagesCount=page1.getPages();
        model.addAttribute("pagesCount",pagesCount);
        return "/customerlist";

    }

    @RequestMapping("/insert")
    public String insert(Model model, CustomerInfo customer, HttpSession session){
        CustomerInfo customer1=new CustomerInfo();
        customer1.setRealname(customer.getRealname());
        customer1.setCustomername(customer.getCustomername());
        customer1.setPhone(customer.getPhone());
        customer1.setEmail(customer.getEmail());
        customer1.setCompany(customer.getCompany());
        customer1.setChannel("客服录入");
        customer1.setLevel("普通客户");
        LocalDateTime newLDT = LocalDateTime.now();
        customer1.setCreatetime(newLDT);
        customer1.setUpdatetime(newLDT);
        CustomerService cs=(CustomerService) session.getAttribute("cs");
        customer1.setCustomerserviceid(cs.getCustomerserviceid());
        iCustomerInfoService.save(customer1);
        return "redirect:/customerInfo/index?value=&current=1&size=5";
    }

    @RequestMapping("/update")  //@RequestParam("customerserviceid") Integer customerserviceid
    public String update(Model model, Integer id, @RequestParam("current") Integer current, @RequestParam("size") Integer size){
        CustomerInfo c=iCustomerInfoService.getById(id);
        model.addAttribute("customer",c);
        System.out.println(c.getChannel());

        return "updatecustomer";
    }

    @RequestMapping("/doupdate")  //@RequestParam("customerserviceid") Integer customerserviceid
    public String update(Model model, @RequestParam("current") Integer current, @RequestParam("size") Integer size, CustomerInfo customer){
        customer.setUpdatetime(LocalDateTime.now());
        iCustomerInfoService.updateById(customer);
        List<CustomerInfo> list=iCustomerInfoService.list();

        IPage<CustomerInfo> page=new Page<>(current,size); //表
        IPage<CustomerInfo> page1=iCustomerInfoService.page(page);
        List<CustomerInfo> list2=page1.getRecords();
        long pagesCount=page1.getPages();

        model.addAttribute("list",list2);
        model.addAttribute("pagesCount",pagesCount);
//        model.addAttribute("list",list);

        return "/customerlist";
    }

    @RequestMapping("/addWorkOder")  //@RequestParam("customerserviceid") Integer customerserviceid
    public String addWorkOrder (Model model, Integer id, @RequestParam("current") Integer current, @RequestParam("size") Integer size){
        model.addAttribute("customerid",id);
        return "addworkorder";
    }

    @RequestMapping("/doAdd")  //@RequestParam("customerserviceid") Integer customerserviceid
    public String doAdd(Model model, HttpSession session, @RequestParam("current") Integer current, @RequestParam("size") Integer size, WorkOrder workOrder){
        CustomerService customerService= (CustomerService) session.getAttribute("cs");
        workOrder.setCustomerserviceid(customerService.getCustomerserviceid());
        LocalDateTime createtime = LocalDateTime.now();
        workOrder.setCreatetime(createtime);
        workOrder.setUpdatetime(createtime);
        workOrder.setChannel("手动录入");
        this.iWorkOrderService.save(workOrder);

        IPage<CustomerInfo> page=new Page<>(current,size); //表
        IPage<CustomerInfo> page1=iCustomerInfoService.page(page);
        List<CustomerInfo> list2=page1.getRecords();
        long pagesCount=page1.getPages();

        model.addAttribute("list",list2);
        model.addAttribute("pagesCount",pagesCount);

        return "/customerlist";
    }

    @RequestMapping("/del")
    public String del(Model model, CustomerInfo customer, @RequestParam("current") Integer current, @RequestParam("size") Integer size){
//        System.out.println("del");
        iCustomerInfoService.removeById(customer.getId());
        List<CustomerInfo> list=iCustomerInfoService.list();

        IPage<CustomerInfo> page=new Page<>(current,size); //表
        IPage<CustomerInfo> page1=iCustomerInfoService.page(page);
        List<CustomerInfo> list2=page1.getRecords();
        long pagesCount=page1.getPages();

        model.addAttribute("list",list2);
        model.addAttribute("pagesCount",pagesCount);
//        model.addAttribute("list",list);

        return "/customerlist";
    }


    @RequestMapping("/getOne")
    public String getOne(Model model, HttpSession session, Integer id, Integer state, Integer current){
        QueryWrapper<WorkOrder> qu=new QueryWrapper<>();
        qu.eq("customerid", id);
        int ordercnt=iWorkOrderService.count(qu);
        model.addAttribute("count", ordercnt);
        CustomerInfo c=iCustomerInfoService.getById(id);
        model.addAttribute("customer",c);
        String NAME=c.getRealname();
//        Integer state;  //该用户是否被此客服拉黑，若被拉黑返回2，未被拉黑返回0
        if(state==-1){
            QueryWrapper<BlackList> qw=new QueryWrapper<>();
            CustomerService cs=(CustomerService) session.getAttribute("cs");
            qw.eq("customerserviceid",cs.getCustomerserviceid());
            qw.eq("customerid",id);
            int count=iBlackListService.count(qw);
            if(count==0) state=0;
            else state=2;
        }
        model.addAttribute("state", state);


        QueryWrapper<SessionMessage> qw = new QueryWrapper<>();
        CustomerService customerService = (CustomerService) session.getAttribute("cs");
        qw.eq("customerserviceid", customerService.getCustomerserviceid());
        qw.eq("customerId", id);
        session.setAttribute("NAME", NAME);
        session.setAttribute("cusSerID", customerService.getCustomerserviceid());
        session.setAttribute("cusID", id);
        List<SessionMessage> messageList2 = this.iSessionMessageService.list(qw);
        model.addAttribute("messageList2", messageList2);

        return "customerinfo";
    }

    @RequestMapping("/insertmessage")
    public String insertmessage(HttpSession session,@RequestParam("cusID") Integer cusID,@RequestParam("cusSerID") Integer cusSerID,@RequestParam("value")String value,@RequestParam("current")Integer current){

        LocalDateTime newLDT = LocalDateTime.now();

        int year = newLDT.getYear();//年
        int month = newLDT.getMonth().getValue();//月
        int dayOfMonth = newLDT.getDayOfMonth();//日
        int hour = newLDT.getHour();//时
        int minute = newLDT.getMinute();//分
        int second = newLDT.getSecond();
        SessionMessage sessionMessage=new SessionMessage(cusSerID,false,newLDT,value,cusID);
        boolean i= iSessionMessageService.save(sessionMessage);
        String name=iCustomerInfoService.getById(cusID).getRealname();

        session.setAttribute("name",name);
        return "redirect:/customerInfo/getOne?id="+cusID+"&state=-1&current="+current;
    }

    @RequestMapping("/getCustomerOrder")
    public String getCustomerOrder(HttpSession session,Model model, int id, @RequestParam("value")String value,@RequestParam("current")Integer current, @RequestParam("current1")Integer current1, @RequestParam("size")Integer size){
        IPage<WorkOrder> page=new Page<>(current1,size);
        String customerName=iCustomerInfoService.getById(id).getRealname();
        QueryWrapper<WorkOrder> qu=new QueryWrapper();
        qu.eq("customerid", id);
        qu.like("title", value);
        IPage<WorkOrder> page1=iWorkOrderService.page(page,qu);
        List<WorkOrder> list=page1.getRecords();
        long pagesCount = page1.getPages();
        for(WorkOrder wo:list){
            QueryWrapper qw=new QueryWrapper();
            qw.eq("customerserviceid",wo.getCustomerserviceid());
            wo.setCustomerservicename(iCustomerServiceService.getOne(qw).getRealname());
        }

        for(WorkOrder wo:list){
            QueryWrapper qw=new QueryWrapper();
            qw.eq("id",wo.getCustomerid());
            wo.setCustomername(customerName);
        }
        model.addAttribute("list", list);
        model.addAttribute("pagesCount",pagesCount);
        model.addAttribute("customerName",customerName);
        return "customerorder";
    }

//    拉黑 state0:用户未被拉黑，state1:拉黑用户后显示用户拉黑成功，state2:用户已拉黑，state3:点击解除拉黑后显示是否确定解除
    @RequestMapping("/blackListOperate")
    public String pullBlack(Model model, HttpSession session, Integer id, Integer state, Integer current){
        CustomerService cs=(CustomerService) session.getAttribute("cs");
        if(state==0){
            BlackList b = new BlackList(cs.getCustomerserviceid(), id);
            b.setTime(LocalDateTime.now());
            this.iBlackListService.save(b);
            System.out.println("添加黑名单成功");
            return "redirect:/customerInfo/getOne?id="+id+"&state=1&current="+current;
        } else if(state==3) {
            QueryWrapper<BlackList> qw=new QueryWrapper<>();
            qw.eq("customerserviceid",cs.getCustomerserviceid());
            qw.eq("customerid",id);
            iBlackListService.remove(qw);
            return "redirect:/customerInfo/getOne?id="+id+"&state=0&current="+current;
        }  else {
            return "redirect:/customerInfo/getOne?id="+id+"&state=3&current="+current;
        }
    }
}

